home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Complementary Applications 2004 February / SGI IRIX 6.5 Complementary Applications 2004 February.iso / dist / cde.idb / usr / dt / share / examples / dtwsm / occupy.c.z / occupy.c
Encoding:
C/C++ Source or Header  |  2003-11-18  |  11.8 KB  |  488 lines

  1. /*
  2.  * occupy.c
  3.  *
  4.  * Copyright 2000, Silicon Graphics, Inc.
  5.  * ALL RIGHTS RESERVED
  6.  * 
  7.  * UNPUBLISHED -- Rights reserved under the copyright laws of the United
  8.  * States.   Use of a copyright notice is precautionary only and does not
  9.  * imply publication or disclosure.
  10.  *
  11.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
  12.  * Use, duplication or disclosure by the Government is subject to restrictions
  13.  * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
  14.  * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
  15.  * in similar or successor clauses in the FAR, or the DOD or NASA FAR
  16.  * Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
  17.  * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
  18.  *
  19.  * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
  20.  * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
  21.  * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
  22.  * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
  23.  * GRAPHICS, INC.
  24.  */
  25. /* $XConsortium: occupy.c /main/cde1_maint/1 1995/07/17 16:46:25 drk $ */
  26. /*
  27.  *  (c) Copyright 1993, 1994 Hewlett-Packard Company    
  28.  *  (c) Copyright 1993, 1994 International Business Machines Corp.
  29.  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  30.  *  (c) Copyright 1993, 1994 Novell, Inc.
  31.  */
  32.  
  33.  
  34. /* Demonstrate the DtWsm Workspace API. */
  35.  
  36. /*  
  37.  * include files  
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include <Xm/XmAll.h>
  42. #include <Dt/Wsm.h>
  43.  
  44.  
  45. /*  
  46.  * functions
  47.  */
  48.  
  49. void main();
  50. static void quitCB(); 
  51. static void setCB(); 
  52. static void showCB(); 
  53. static void wsCB(); 
  54. static void allWsCB(); 
  55. static void SetupWorkspaceButtons ();
  56. static void ShowWorkspaceOccupancy();
  57. static void SetWorkspaceOccupancy();
  58.  
  59. /*  
  60.  * global variables  
  61.  */
  62.  
  63. static XmString xms;
  64. static Widget toplevel;
  65. static Widget wChildren [20];
  66. static Widget *wWs;
  67. static Atom *paWsSet = NULL;
  68. static Boolean *pbOccupy;
  69. static unsigned long numWorkspaces;
  70. static Atom *paWs;
  71.  
  72. /* 
  73.  * button assignments 
  74.  */
  75. #define QUIT_BUTTON    (wChildren[0])
  76. #define SEPARATOR    (wChildren[1])
  77. #define SET_BUTTON      (wChildren[2])
  78. #define SHOW_BUTTON     (wChildren[3])
  79. #define ALL_WS_BUTTON   (wChildren[4])
  80. #define NUM_CHILDREN    5
  81.  
  82. /*
  83.  * main - main logic for program
  84.  */
  85.  
  86. void main (argc,argv)
  87.     int argc;
  88.     char **argv;
  89. {
  90.     Widget frame;
  91.     Widget form;
  92.     Widget  top;
  93.     XtAppContext app_context;
  94.     Arg    args[10];
  95.     int n;
  96.     int i;
  97.  
  98.     /*
  99.      * initialize toolkit
  100.      */
  101.     n = 0;
  102.     XtSetArg (args[n], XmNallowShellResize, True);  n++;
  103.     toplevel = XtAppInitialize (&app_context, "Dtoccupy", NULL, 0, &argc, argv,
  104.                                  NULL, args, n);
  105.  
  106.     n = 0;
  107.     XtSetArg (args[n], XmNmarginWidth, 10);  n++;
  108.     XtSetArg (args[n], XmNmarginHeight, 10);  n++;
  109.     frame = XmCreateFrame (toplevel, "frame", args, n);
  110.     XtManageChild (frame);
  111.  
  112.     /*
  113.      *  manager widget
  114.      */
  115.     n = 0;
  116.     form = XmCreateForm (frame, "form", args, n);
  117.     XtManageChild (form);
  118.  
  119.     /*
  120.      * create compound string for the button text
  121.      */
  122.     xms = XmStringCreateLocalized("Quit");
  123.  
  124.     /*
  125.      * set up arglist
  126.      */
  127.     n = 0;
  128.     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
  129.     XtSetArg (args[n], XmNlabelString, xms);  n++;
  130.     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  131.     XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  132.  
  133.     /*
  134.      * create button
  135.      */
  136.     top = QUIT_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
  137.                                 form, args, n);
  138.  
  139.     XmStringFree(xms);
  140.     /*
  141.      * add callback
  142.      */
  143.     XtAddCallback (QUIT_BUTTON, XmNactivateCallback, quitCB, NULL);
  144.  
  145.     /*  
  146.      * create compound string for the button text  
  147.      */
  148.     xms = XmStringCreateLocalized("Set Occupancy");
  149.  
  150.     /*  
  151.      * set up arglist  
  152.      */
  153.     n = 0;
  154.     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
  155.     XtSetArg (args[n], XmNlabelString, xms);  n++;
  156.     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  157.     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  158.     XtSetArg (args[n], XmNtopWidget, top); n++;
  159.  
  160.     /*  
  161.      * create button  
  162.      */
  163.     top = SET_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
  164.       form, args, n);
  165.  
  166.     XmStringFree(xms);
  167.     /*  
  168.      * add callback  
  169.      */
  170.     XtAddCallback (SET_BUTTON, XmNactivateCallback, setCB, NULL);
  171.  
  172.     /*  
  173.      * create compound string for the button text  
  174.      */
  175.     xms = XmStringCreateLocalized("Occupy All Workspaces");
  176.  
  177.     /*  
  178.      * set up arglist  
  179.      */
  180.     n = 0;
  181.     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
  182.     XtSetArg (args[n], XmNlabelString, xms);  n++;
  183.     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  184.     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  185.     XtSetArg (args[n], XmNtopWidget, top); n++;
  186.     /*  
  187.      * create button  
  188.      */
  189.     top = ALL_WS_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
  190.       form, args, n);
  191.  
  192.     XmStringFree(xms);
  193.  
  194.     /*  
  195.      * add callback  
  196.      */
  197.     XtAddCallback (ALL_WS_BUTTON, XmNactivateCallback, allWsCB, NULL);
  198.  
  199.     /*  
  200.      * create compound string for the button text  
  201.      */
  202.     xms = XmStringCreateLocalized("Show Occupancy");
  203.  
  204.     /*  
  205.      * set up arglist  
  206.      */
  207.     n = 0;
  208.     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
  209.     XtSetArg (args[n], XmNlabelString, xms);  n++;
  210.     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  211.     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  212.     XtSetArg (args[n], XmNtopWidget, top); n++;
  213.     /*   
  214.      * create button  
  215.      */
  216.     top = SHOW_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
  217.       form, args, n);
  218.  
  219.     XmStringFree(xms);
  220.     /*  
  221.      * add callback  
  222.      */
  223.     XtAddCallback (SHOW_BUTTON, XmNactivateCallback, showCB, NULL);
  224.  
  225.     /*  
  226.      * set up arglist  
  227.      */
  228.     n = 0;
  229.     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  230.     XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  231.     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  232.     XtSetArg (args[n], XmNtopWidget, top); n++;
  233.     /*  
  234.      * create separator  
  235.      */
  236.     top = SEPARATOR = (Widget) 
  237.         XmCreateSeparatorGadget (form, "separator", args, n);
  238.  
  239.     /* 
  240.      * set up workspace buttons 
  241.      */
  242.     SetupWorkspaceButtons (form, top);
  243.  
  244.     /*  
  245.      * manage children
  246.      */
  247.     XtManageChildren (wChildren, NUM_CHILDREN);
  248.     XtManageChildren (wWs, numWorkspaces);
  249.  
  250.     /*  
  251.      * realize widgets  
  252.      */
  253.     XtRealizeWidget (toplevel);
  254.  
  255.     /* 
  256.      * set up initial workspace occupancy info 
  257.      */
  258.     pbOccupy = (Boolean *) XtMalloc (numWorkspaces * sizeof (Boolean));
  259.     for (i=0; i<numWorkspaces; i++)
  260.     {
  261.     pbOccupy[i] = False;
  262.     }
  263.     ShowWorkspaceOccupancy ();
  264.  
  265.     /*  
  266.      * process events  
  267.      */
  268.     XtAppMainLoop (app_context);
  269. }
  270.  
  271. /*-------------------------------------------------------------
  272.  *    Create a set of buttons for the current set of workspaces
  273.  */
  274. static void SetupWorkspaceButtons (form, top)
  275. Widget form;
  276. Widget top;
  277. {
  278.     Window root;
  279.     Arg    args[10]; /*  arg list        */
  280.     register int n;    /*  arg count        */
  281.     register int i;
  282.     DtWsmWorkspaceInfo *pWsInfo;
  283.     char *pchWs;
  284.     Widget left = None;
  285.     Widget *pWid;
  286.  
  287.     /*  
  288.      * get workspace information 
  289.      */
  290.     root = XRootWindowOfScreen(XtScreen(form));
  291.     if (DtWsmGetWorkspaceList (XtDisplay(form), root, 
  292.             &paWs, (int *)&numWorkspaces) == Success)
  293.     {
  294.     wWs = (Widget *) XtMalloc (numWorkspaces * sizeof (Widget));
  295.     pWid = wWs;
  296.     for (i=0; i<numWorkspaces; i++, pWid++)
  297.     {
  298.         DtWsmGetWorkspaceInfo (XtDisplay(form), root, paWs[i],
  299.         &pWsInfo);
  300.  
  301.         pchWs = (char *) XmGetAtomName (XtDisplay(form), 
  302.                         pWsInfo->workspace);
  303.         /*  
  304.          * create compound string for the button text  
  305.          */
  306.         xms = XmStringCreateLocalized(pchWs);
  307.  
  308.         /*  
  309.          * set up arglist  
  310.          */
  311.         n = 0;
  312.         XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
  313.         XtSetArg (args[n], XmNlabelString, xms);  n++;
  314.         XtSetArg (args[n], XmNindicatorOn, True);  n++;
  315.         XtSetArg (args[n], XmNindicatorType, XmN_OF_MANY);  n++;
  316.         if (left == None)
  317.         {
  318.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  319.         }
  320.         else
  321.         {
  322.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  323.         XtSetArg (args[n], XmNleftWidget, left); n++;
  324.         }
  325.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  326.         XtSetArg (args[n], XmNtopWidget, top); n++;
  327.         /*  
  328.          * create button  
  329.          */
  330.         *pWid = XtCreateWidget ("button", xmToggleButtonWidgetClass,
  331.           form, args, n);
  332.  
  333.         XmStringFree(xms);
  334.         /*  
  335.          * add callback  
  336.          */
  337.         XtAddCallback (*pWid, XmNvalueChangedCallback, wsCB, 
  338.                     (XtPointer) i);
  339.         left = *pWid;
  340.     }
  341.     }
  342.     else
  343.     {
  344.     fprintf(stderr, "Failed to get workspace info -- dtwm not running!\n");
  345.     exit (1);
  346.     }
  347. }
  348.  
  349. /*-------------------------------------------------------------
  350.  *    Update the workspace buttons with where we are now.
  351.  */
  352. static void ShowWorkspaceOccupancy ()
  353. {
  354.     Arg    args[10]; /*  arg list        */
  355.     register int n;    /*  arg count        */
  356.     int i,j;
  357.     Atom *paWsIn = NULL;
  358.     unsigned long numWsIn;
  359.     int rval;
  360.  
  361.     rval = DtWsmGetWorkspacesOccupied(XtDisplay(toplevel), 
  362.         XtWindow(toplevel), &paWsIn, &numWsIn );
  363.  
  364.     if (rval == Success)
  365.     {
  366.  
  367.     for (i = 0; i<numWorkspaces; i++)
  368.     {
  369.         pbOccupy[i] = False;
  370.         for (j=0; j<numWsIn; j++)
  371.         {
  372.         if (paWs[i] == paWsIn[j])
  373.         {
  374.             pbOccupy[i] = True;
  375.             break;
  376.         }
  377.         }
  378.     }
  379.  
  380.     for (i = 0; i<numWorkspaces; i++)
  381.     {
  382.         n = 0;
  383.         if (pbOccupy[i])
  384.         {
  385.         XtSetArg (args[n], XmNset, True);  n++;
  386.         }
  387.         else
  388.         {
  389.         XtSetArg (args[n], XmNset, False);  n++;
  390.         }
  391.         XtSetValues (wWs[i], args, n);
  392.     }
  393.  
  394.     if (paWsIn) XFree ((char *) paWsIn);
  395.     }
  396. }
  397.  
  398. /*-------------------------------------------------------------
  399.  *    quitCB - callback for button
  400.  */
  401. static void quitCB (w, client_data, call_data) 
  402. Widget    w;        /*  widget id        */
  403. XtPointer    client_data;    /*  data from application   */
  404. XtPointer    call_data;    /*  data from widget class  */
  405. {
  406.     exit (0);
  407. }
  408.  
  409.  
  410. /*-------------------------------------------------------------
  411.  *    showCB - callback for show occupancy button
  412.  */
  413. static void showCB (w, client_data, call_data) 
  414. Widget    w;        /*  widget id        */
  415. XtPointer    client_data;    /*  data from application   */
  416. XtPointer    call_data;    /*  data from widget class  */
  417. {
  418.  
  419.     ShowWorkspaceOccupancy();
  420.  
  421. }
  422.  
  423. /*-------------------------------------------------------------
  424.  *    setCB - callback for set occupancy button
  425.  */
  426. static void setCB (w, client_data, call_data) 
  427. Widget    w;        /*  widget id        */
  428. XtPointer    client_data;    /*  data from application   */
  429. XtPointer    call_data;    /*  data from widget class  */
  430. {
  431.     int numSet;
  432.     int i;
  433.  
  434.     if (!paWsSet)
  435.     {
  436.     paWsSet = (Atom *) XtMalloc (numWorkspaces * sizeof(Atom));
  437.     }
  438.  
  439.     for (i=0, numSet=0; i<numWorkspaces; i++)
  440.     {
  441.     if (pbOccupy[i])
  442.     {
  443.         paWsSet[numSet++] = paWs[i];
  444.     }
  445.     }
  446.  
  447.     DtWsmSetWorkspacesOccupied (XtDisplay(toplevel),
  448.                 XtWindow(toplevel), paWsSet, numSet);
  449.  
  450. }
  451.  
  452. /*-------------------------------------------------------------
  453.  *    wsCB - callback for set occupancy button
  454.  */
  455. static void wsCB (w, client_data, call_data) 
  456. Widget    w;        /*  widget id        */
  457. XtPointer    client_data;    /*  data from application   */
  458. XtPointer    call_data;    /*  data from widget class  */
  459. {
  460.     int i = (Atom) client_data; /* ws array index */
  461.     XmToggleButtonCallbackStruct *ptb = 
  462.         (XmToggleButtonCallbackStruct *) call_data;
  463.  
  464.     if (ptb->reason == XmCR_VALUE_CHANGED)
  465.     {
  466.     if (ptb->set)
  467.     {
  468.         pbOccupy[i] = True;
  469.     }
  470.     else
  471.     {
  472.         pbOccupy[i] = False;
  473.     }
  474.     }
  475. }
  476.  
  477. /*-------------------------------------------------------------
  478.  *    allWsCB - callback for occupy all button
  479.  */
  480. static void allWsCB (w, client_data, call_data) 
  481. Widget    w;        /*  widget id        */
  482. XtPointer    client_data;    /*  data from application   */
  483. XtPointer    call_data;    /*  data from widget class  */
  484. {
  485.     DtWsmOccupyAllWorkspaces (XtDisplay(toplevel), XtWindow(toplevel));
  486. }
  487.  
  488.